package com.xnyzc.lhy.mis.service.impl.activity;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xnyzc.lhy.common.exception.ActivityException;
import com.xnyzc.lhy.common.penum.activity.ActivityErrorCode;
import com.xnyzc.lhy.common.util.IDUtil;
import com.xnyzc.lhy.common.util.TokenUtil;
import com.xnyzc.lhy.mis.entity.activity.OaSysActivityRewardRule;
import com.xnyzc.lhy.mis.entity.param.activity.ActivityRewardRuleCreateParam;
import com.xnyzc.lhy.mis.entity.result.activity.ActivityInfoRewardRuleResult;
import com.xnyzc.lhy.mis.mapper.activity.OaSysActivityRewardRuleMapper;
import com.xnyzc.lhy.mis.service.activity.IOaSysActivityRewardRuleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 奖励方式 服务实现类
 * </p>
 *
 * @author haoyfa
 * @since 2019-12-16
 */
@Service
@Slf4j
public class OaSysActivityRewardRuleServiceImpl extends ServiceImpl<OaSysActivityRewardRuleMapper, OaSysActivityRewardRule> implements IOaSysActivityRewardRuleService {
    @Autowired
    OaSysActivityRewardRuleMapper oaSysActivityRewardRuleMapper;

    /**
     * 创建活动规则记录
     * @param defId
     * @param activityRewardRuleList
     */
    @Override
    public void createActivityRewardRule(Long defId, Date createDate, List<ActivityRewardRuleCreateParam> activityRewardRuleList) {
        if(activityRewardRuleList == null
                ||activityRewardRuleList.size() <=0)
        {
            log.error(ActivityErrorCode.ruleEmptyError.getDesc());
            throw new ActivityException(ActivityErrorCode.ruleEmptyError);
        }

        List<OaSysActivityRewardRule> oaSysActivityRewardRuleList = new ArrayList<>();
        for (ActivityRewardRuleCreateParam activityRewardRule: activityRewardRuleList) {
            OaSysActivityRewardRule oaSysActivityRewardRule = new OaSysActivityRewardRule();
            oaSysActivityRewardRule.setDefId(defId);

            Long id = IDUtil.nextId();
            oaSysActivityRewardRule.setId(id);
            //司机来源。00=推荐人；01=被推荐人
            oaSysActivityRewardRule.setDriverFrom(activityRewardRule.getDriverFrom());
            //考核指标。00=完成订单量；01=订单流水金额；02=出车时长。
            oaSysActivityRewardRule.setRewardType(activityRewardRule.getRewardType());
            //达成数量
            oaSysActivityRewardRule.setRewardNumber(new BigDecimal(activityRewardRule.getRewardNumber()));
            //奖励金额
            oaSysActivityRewardRule.setRewardAmount(new BigDecimal(activityRewardRule.getRewardAmount()));

            oaSysActivityRewardRule.setCreateTime(createDate);
            oaSysActivityRewardRule.setCreateUser(TokenUtil.getTokenUserId());
            oaSysActivityRewardRule.setDeleted(0);
            oaSysActivityRewardRule.setVersion(0L);
            oaSysActivityRewardRule.setUpdateTime(createDate);

            oaSysActivityRewardRuleList.add(oaSysActivityRewardRule);
        }

        if(oaSysActivityRewardRuleList.size() > 0) {
            this.saveBatch(oaSysActivityRewardRuleList);
        }
    }

    /**
     * 根据活动定义id查询活动奖励方式
     * @param defId
     * @return
     */
    @Override
    public List<OaSysActivityRewardRule> queryActivityRewardRuleList(Long defId){
        QueryWrapper<OaSysActivityRewardRule> osSysActivityRewardRuleQueryWrapper = new QueryWrapper<OaSysActivityRewardRule>();
        osSysActivityRewardRuleQueryWrapper.eq(OaSysActivityRewardRule.DEF_ID, defId);
        //根据达成数量进行正序排列
        osSysActivityRewardRuleQueryWrapper.orderByAsc(OaSysActivityRewardRule.REWARD_NUMBER);

        return oaSysActivityRewardRuleMapper.selectList(osSysActivityRewardRuleQueryWrapper);
    }

    /**
     * 根据活动定义id查询活动奖励方式result
     * @param defId
     * @return
     */
    @Override
    public List<ActivityInfoRewardRuleResult> queryActivityRewardRuleResultList(Long defId){
        List<OaSysActivityRewardRule> oaSysActivityRewardRuleList = this.queryActivityRewardRuleList(defId);

        if(oaSysActivityRewardRuleList != null
                && oaSysActivityRewardRuleList.size() >= 0){
            List<ActivityInfoRewardRuleResult> activityInfoRewardRuleResultList =
                    new ArrayList<ActivityInfoRewardRuleResult>(oaSysActivityRewardRuleList.size());

            for (OaSysActivityRewardRule oaSysActivityRewardRule : oaSysActivityRewardRuleList) {
                ActivityInfoRewardRuleResult activityInfoRewardRuleResult = new ActivityInfoRewardRuleResult();
                activityInfoRewardRuleResult.setId(oaSysActivityRewardRule.getId().toString());
                activityInfoRewardRuleResult.setDriverFrom(oaSysActivityRewardRule.getDriverFrom());
                activityInfoRewardRuleResult.setRewardType(oaSysActivityRewardRule.getRewardType());
                activityInfoRewardRuleResult.setRewardNumber(oaSysActivityRewardRule.getRewardNumber().toString());
                activityInfoRewardRuleResult.setRewardAmount(oaSysActivityRewardRule.getRewardAmount().toString());

                activityInfoRewardRuleResultList.add(activityInfoRewardRuleResult);
            }

            return activityInfoRewardRuleResultList;
        }
        else
        {
            return null;
        }
    }
}
